package edu.ynu.se.xiecheng.achitectureclass.controller;

import edu.ynu.se.xiecheng.achitectureclass.common.controller.LogicController;
import edu.ynu.se.xiecheng.achitectureclass.dao.EquipmentDao;
import edu.ynu.se.xiecheng.achitectureclass.entity.Equipment;
import edu.ynu.se.xiecheng.achitectureclass.service.EquipmentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.Map;

@Api(tags = "设备管理接口")
@RestController
@RequestMapping("/equipment")
public class EquipmentController extends LogicController<EquipmentService, EquipmentDao, Equipment, Long> {

    public EquipmentController(@Autowired EquipmentService service) {
        super(service);
    }

    private Map<String, Object> formatEquipmentData(Equipment equipment) {
        Map<String, Object> equipmentData = new HashMap<>();
        equipmentData.put("id", equipment.getId());
        equipmentData.put("equipmentName", equipment.getEquipmentName());
        equipmentData.put("equipmentSerial", equipment.getEquipmentSerial());
        equipmentData.put("fboxNumber", equipment.getFboxNumber());
        equipmentData.put("equipmentType", equipment.getEquipmentType());
        equipmentData.put("measurementUnit", equipment.getMeasurementUnit());
        equipmentData.put("videoStreamUrl", equipment.getVideoStreamUrl());
        equipmentData.put("createTime", equipment.getCreateTime());
        equipmentData.put("updateTime", equipment.getUpdateTime());
        return equipmentData;
    }

    @GetMapping("/info")
    @ApiOperation("获取单个设备信息")
    public ResponseEntity<?> getEquipmentInfo(@ApiParam("设备ID") @RequestParam Long id) {
        try {
            Equipment equipment = getService().getEquipmentInfo(id);
            return ResponseEntity.ok(Map.of(
                    "code", 200,
                    "message", "查询成功",
                    "data", formatEquipmentData(equipment)
            ));
        } catch (Exception e) {
            return ResponseEntity.ok(Map.of(
                    "code", 400,
                    "message", e.getMessage()
            ));
        }
    }

    @PostMapping("/update")
    @ApiOperation("更新设备信息")
    public ResponseEntity<?> updateEquipment(@Valid @RequestBody Equipment equipment) {
        try {
            Equipment updatedEquipment = getService().updateEquipment(equipment);
            return ResponseEntity.ok(Map.of(
                    "code", 200,
                    "message", "更新成功",
                    "data", formatEquipmentData(updatedEquipment)
            ));
        } catch (Exception e) {
            return ResponseEntity.ok(Map.of(
                    "code", 400,
                    "message", e.getMessage()
            ));
        }
    }

    @PostMapping("/save")
    @ApiOperation("保存设备信息")
    public ResponseEntity<?> saveEquipment(@Valid @RequestBody Map<String, Object> requestData) {
        try {
            Equipment equipment = new Equipment();
            equipment.setEquipmentName((String) requestData.get("equipmentName"));
            equipment.setEquipmentSerial((String) requestData.get("equipmentSerial"));
            equipment.setFboxNumber((String) requestData.get("fboxNumber"));
            equipment.setEquipmentType((String) requestData.get("equipmentType"));
            equipment.setMeasurementUnit((String) requestData.get("measurementUnit"));
            equipment.setVideoStreamUrl((String) requestData.get("videoStreamUrl"));

            Long plantationId = Long.valueOf(requestData.get("plantation_id").toString());

            Equipment savedEquipment = getService().saveEquipment(equipment, plantationId);
            return ResponseEntity.ok(Map.of(
                    "code", 200,
                    "message", "保存成功",
                    "data", formatEquipmentData(savedEquipment)
            ));
        } catch (Exception e) {
            return ResponseEntity.ok(Map.of(
                    "code", 400,
                    "message", e.getMessage()
            ));
        }
    }

    @PostMapping("/initialize-sensors")
    @ApiOperation("初始化传感器设备")
    public ResponseEntity<?> initializeSensors(@RequestParam Long plantationId) {
        try {
            getService().initializeSensors(plantationId);
            return ResponseEntity.ok(Map.of(
                    "code", 200,
                    "message", "传感器初始化成功"
            ));
        } catch (Exception e) {
            return ResponseEntity.ok(Map.of(
                    "code", 400,
                    "message", e.getMessage()
            ));
        }
    }
}